package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.ContextMenu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.view.tag.FlowView;
import com.banciyuan.bcywebview.base.view.tag.TagView;
import com.banciyuan.bcywebview.base.view.textview.LetterSpaceTextView;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.regex.MatchFilterImpl;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;

import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.Role;
import de.greenrobot.daoexample.model.TagDetail;

/**
 * Created by squall on 2016/8/10.
 */
public class InfoHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {

    private TextView title_tv;
    private TextView work_tv;
    private TextView intro_tv;
    private FlowView character_fv;
    private FlowView tag_fv;
    private LinearLayout charatcter_ll;
    private RelativeLayout main_rl;
    private RelativeLayout role_rl;
    private RelativeLayout tag_rl;
    private LinearLayout container_ll;
    private TagView dailyTagsView;

    private Context mContext;

    public InfoHolder(View itemView) {
        super(itemView);
        title_tv = (TextView) itemView.findViewById(R.id.tv_detail_title);
        work_tv = (TextView) itemView.findViewById(R.id.detail_work_tv);
        intro_tv = (TextView) itemView.findViewById(R.id.detail_intro_tv);
        character_fv = (FlowView) itemView.findViewById(R.id.fv_character);
        charatcter_ll = (LinearLayout) itemView.findViewById(R.id.ll_charatcter_names);
        tag_fv = (FlowView) itemView.findViewById(R.id.fv_tag);
        role_rl = (RelativeLayout) itemView.findViewById(R.id.ll_role);
        main_rl = (RelativeLayout) itemView.findViewById(R.id.detail_character_rly);
        tag_rl = (RelativeLayout) itemView.findViewById(R.id.rl_tag);
        container_ll = (LinearLayout) itemView.findViewById(R.id.info_container);
        dailyTagsView = (TagView) itemView.findViewById(R.id.float_tags);
    }


    public void setData(final Complex mItem, final Context mContext) {
        this.mContext = mContext;
        if (!TextUtils.isEmpty(mItem.getWork())) {
            ((View) (work_tv.getParent())).setVisibility(View.VISIBLE);
            work_tv.setText(mItem.getWork());
            if (TextUtils.isEmpty(mItem.getWid())) {
                work_tv.setTextColor(mContext.getResources().getColor(R.color.recommend_main));
            } else {
                work_tv.setTextColor(mContext.getResources().getColor(R.color.timeline_green));
                work_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(mItem.getWid()))
                            gotoUtil.gotoAct(mContext, CircleSmoothActivity.class, mItem.getWork(), mItem.getWid());
                    }
                });
            }
        } else {
            ((View) (work_tv.getParent())).setVisibility(View.GONE);
        }
        if (StringUtil.notNullEqual(mItem.getType(), "artwork"))
            ((View) (work_tv.getParent())).setVisibility(View.GONE);
        if (TextUtils.isEmpty(mItem.getTitle())) {
            title_tv.setVisibility(View.GONE);
        } else {
            title_tv.setVisibility(View.VISIBLE);
            title_tv.setText(Html.fromHtml(mItem.getTitle()));
        }
        if (mItem.getTimelineType() == TimelineFactory.TIMELINE_TYPE_DAILY) {
            setDailyIntro(mItem, mContext);
        } else {
            setIntro(mItem, mContext);
        }
        List<Role> rlist = mItem.getProperties().getCharacter();
        if (rlist != null && rlist.size() > 0) {
            role_rl.setVisibility(View.VISIBLE);
        } else {
            role_rl.setVisibility(View.GONE);
        }
        character_fv.setHorizontalSpacing(UIUtils.dip2px(12, mContext));
        character_fv.setVerticalSpacing(UIUtils.dip2px(5, mContext));
        character_fv.removeAllViews();
        charatcter_ll.removeAllViews();
        switch (mItem.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                character_fv.setVisibility(View.VISIBLE);
                charatcter_ll.setVisibility(View.GONE);
                dailyTagsView.setVisibility(View.GONE);
                for (int i = 0; i < rlist.size(); i++) {
                    String name = rlist.get(i).getRole_name();
                    TextView tvCharacterName = (TextView) View.inflate(mContext, R.layout.detail_character_view, null);
                    tvCharacterName.setText(name);
                    character_fv.addView(tvCharacterName);
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_COSER:
                character_fv.setVisibility(View.GONE);
                charatcter_ll.setVisibility(View.VISIBLE);
                dailyTagsView.setVisibility(View.GONE);
                for (int i = 0; i < rlist.size(); i++) {
                    Role r = rlist.get(i);
                    TextView tvCharacterName = (TextView) View.inflate(mContext, R.layout.detail_character_view, null);
                    if (i != rlist.size() - 1) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-2, -2);
                        params.bottomMargin = UIUtils.dip2px(3, mContext);
                        tvCharacterName.setLayoutParams(params);
                    }
                    tvCharacterName.setText(Html.fromHtml(String.format(MyConstants.CN_FORMAT, r.getRole_name(), r.getCoser_name())));
                    charatcter_ll.addView(tvCharacterName);
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                main_rl.setVisibility(View.GONE);
                title_tv.setVisibility(View.GONE);
                dailyTagsView.setVisibility(View.VISIBLE);
                dailyTagsView.setTlist(mItem.getRela_circle());
                dailyTagsView.setTagViewClick(new TagView.OnTagClickListener() {
                    @Override
                    public void onClick(TagDetail t) {
                        if (!TextUtils.isEmpty(t.getWork())) {
                            if (!TextUtils.isEmpty(t.getWid())) {
                                Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getWork());
                                intent.putExtra(gotoUtil.INTENT_VALUE_TWO, t.getWid());
                                mContext.startActivity(intent);
                            }
                        } else if (!TextUtils.isEmpty(t.getEvent_id())) {
                            gotoUtil.gotoAct(mContext, EventDetailActivity.class, t.getEvent_id());
                        } else {
                            Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                            intent.putExtra("type", "tag");
                            mContext.startActivity(intent);
                        }
                    }
                });
                break;
        }
        if (mItem.getPost_tags() != null && mItem.getPost_tags().size() > 0) {
            tag_rl.setVisibility(View.VISIBLE);
            addTagItem(mItem.getPost_tags(), mContext);
        } else {
            tag_rl.setVisibility(View.GONE);
        }

        title_tv.setOnCreateContextMenuListener(this);
        intro_tv.setOnCreateContextMenuListener(this);
    }

    private void setIntro(Complex mItem, Context mContext) {
        if (TextUtils.isEmpty(mItem.getPlain())) {
            intro_tv.setVisibility(View.GONE);
        } else {
            String intro = StringUtil.htmlDecoder(mItem.getPlain());
            intro_tv.setText(intro);
            MyLinkfy.addLinks(intro_tv, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
            UnderLineUtil.DisUnderLine(mContext, intro_tv);
        }
    }

    private void setDailyIntro(Complex mItem, Context mContext) {
        if (TextUtils.isEmpty(mItem.getPlain())) {
            intro_tv.setVisibility(View.GONE);
        } else {
            String intro = StringUtil.htmlDecoder(mItem.getPlain());
            intro_tv.setMovementMethod(LinkMovementMethod.getInstance());
            SpannableString spannableString = StringUtil.setAtUser(intro, mItem.getAt_users(), mContext);
            MyLinkfy.addLinks(spannableString, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
            intro_tv.setText(spannableString);
            UnderLineUtil.DisUnderLine(mContext, intro_tv);
        }
    }

    protected void addTagItem(final List<TagDetail> tags, final Context mContext) {
        tag_fv.setHorizontalSpacing(UIUtils.dip2px(12, mContext));
        tag_fv.setVerticalSpacing(UIUtils.dip2px(5, mContext));
        LetterSpaceTextView tv;
        if (null == tags || tags.size() == 0) {
            tag_fv.setVisibility(View.GONE);
            return;
        }
        tag_fv.removeAllViews();
        tag_fv.setVisibility(View.VISIBLE);
        for (int i = 0; i < tags.size(); i++) {
            final String tagName = tags.get(i).getTag_name();
            final String event_id = tags.get(i).getEvent_id();
            tv = new LetterSpaceTextView(mContext);
            tv.setTextSize(16);
            tv.setTextColor(mContext.getResources().getColor(R.color.timeline_green));
            tv.setText(tagName);
            if (TextUtils.isEmpty(event_id)) {
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, tagName);
                        intent.putExtra("type", "tag");
                        mContext.startActivity(intent);
                    }
                });
            } else {
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        gotoUtil.gotoAct(mContext, EventDetailActivity.class, event_id);
                    }
                });

            }
            tag_fv.addView(tv);
        }
    }

    public void setGone() {
        container_ll.setVisibility(View.GONE);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        try {
            menu.add(0, v.getId(), 0, mContext.getString(R.string.copy_paste));
            TextView textView = (TextView) v;
            ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("shareText", textView.getText().toString());
            clipboard.setPrimaryClip(clipData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
